package com.example.smart.filter;

import com.example.common.config.JwtHelper;
import com.example.common.util.R;
import com.example.smart.security.AccountAdmin;
import com.example.smart.security.AccountUser;
import com.example.smart.util.RedisCache;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Objects;

@Component
@Slf4j
public class JwtFilter extends OncePerRequestFilter {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private JwtHelper jwtHelper;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //从请求头中获取token
        String jwtToken = request.getHeader("token");
        if (!StringUtils.hasText(jwtToken)) {
            //放行
            filterChain.doFilter(request, response);
            return;
        }
        try {
            Claims claims = jwtHelper.getTokenBody(jwtToken);
            /*String userId = claims.getSubject();
            String userRedisKey = "userLogin:" + userId;
            AccountUser accountUser = redisCache.getCacheObject(userRedisKey);
            if (Objects.isNull(accountUser)) {
                throw new RuntimeException("用户未登录");
            }
            UsernamePasswordAuthenticationToken userAuthenctionToken = new UsernamePasswordAuthenticationToken(accountUser, null, accountUser.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(userAuthenctionToken);
*/
            String adminId = claims.getSubject();
            log.info("=== adminId ===: {}", adminId);
            String adminRedisKey = "adminLogin:" + adminId;
            AccountAdmin accountAdmin = redisCache.getCacheObject(adminRedisKey);
            log.info("accountAdmin: {}", accountAdmin);
            if (Objects.isNull(accountAdmin)) {
                throw new RuntimeException("管理员未登录");
            }
            UsernamePasswordAuthenticationToken adminAuthenctionToken = new UsernamePasswordAuthenticationToken(accountAdmin, null, accountAdmin.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(adminAuthenctionToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //继续过滤
        filterChain.doFilter(request, response);
    }
}